var ddapi = require('/../../../utils/md5.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    area: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.loadArea();
  },

  onShow: function() {
    var that = this
    that.loadArea();
  },

  loadArea: function () {
    var that = this
    var openid = wx.getStorageSync('openid');
    var service = 'User.NewArea';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(openid + service + user_id);
    var quan = [];
    wx.request({
      url: app.globalData.apiUrl + '?service=User.NewArea',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id
      },

      success: function (res5) {
        console.log(res5.data.data.info)
        var arr = res5.data.data.info;
        for (var i = 0; i < arr.length; i++) {
          var dic = arr[i];
          if (dic.area_name != null && dic.area_name != '') {
            quan.push(dic)
          }
        }
        that.setData({
          area: quan
        });
      }
    })
  },
  deleteArea: function (e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否删除' + e.currentTarget.dataset['area_name']+'?',
      success: function (res) {
        if (res.confirm) {
          that.delArea(e.currentTarget.dataset['area_id']);
        } 
      }
    })
  },
  delArea: function (area_id) {
    var that = this
    var service = 'User.DelArea';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var area_id = area_id;
    var sign = ddapi.md5(area_id + openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.DelArea',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        area_id: area_id,
        open_id: openid,
        user_id: user_id
      },
      success: function (res) {
        console.log(res)
        var code = res.data.data.code;
        if (code == 0) {
          that.loadArea();
          wx.showModal({
            title: "提示",
            content: "删除成功",
            showCancel: false
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "删除失败",
            showCancel: false
          })
        }
      }
    })
  },
  pushMap: function () {
    var that = this
  }
})